package com.cg.design.graphicdesignpattern.mediator;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 表示登陆对话框的类，实现了Mediator类和java.awt.event.ActionListener类，继承了java.awt.Frame类
 *
 * @program: LeetCode->LoginFrame
 * @description: 表示登陆对话框的类，实现了Mediator类和java.awt.event.ActionListener类，继承了java.awt.Frame类
 * @author: cg
 * @create: 2022-03-28 10:50
 **/
public class LoginFrame extends Frame implements ActionListener, Mediator {

    /**
     * 游客登陆单选框
     */
    private ColleagueCheckbox checkboxGuest;
    /**
     * 用户登陆单选框
     */
    private ColleagueCheckbox checkboxLogin;
    /**
     * 用户名输入框
     */
    private ColleagueTextField textUser;
    /**
     * 密码输入框
     */
    private ColleagueTextField textPass;
    /**
     * OK按钮
     */
    private ColleagueButton buttonOk;
    /**
     * Cancel按钮
     */
    private ColleagueButton buttonCancel;

    /**
     * 构造函数，生成并配置各个Colleague后，显示对话框
     *
     * @param title
     */
    public LoginFrame(String title) {
        super(title);
        setBackground(Color.lightGray);
        // 使用布局管理器生成4*2窗格
        setLayout(new GridLayout(4, 2));
        // 生成各个Colleague
        createColleagues();
        // 配置
        add(checkboxGuest);
        add(checkboxLogin);
        add(new Label("Username:"));
        add(textUser);
        add(new Label("Password:"));
        add(textPass);
        add(buttonOk);
        add(buttonCancel);
        // 设置初始的启用/禁用状态
        colleagueChanged();
        // 显示
        pack();
        show();
    }

    @Override
    public void createColleagues() {
        // 生成各个Colleague
        CheckboxGroup group = new CheckboxGroup();
        checkboxGuest = new ColleagueCheckbox("Guest", group, true);
        checkboxLogin = new ColleagueCheckbox("Login", group, false);
        textUser = new ColleagueTextField("", 10);
        textPass = new ColleagueTextField("", 10);
        textPass.setEchoChar('*');
        buttonOk = new ColleagueButton("OK");
        buttonCancel = new ColleagueButton("Cancel");
        // 设置Mediator
        checkboxGuest.setMediator(this);
        checkboxLogin.setMediator(this);
        textUser.setMediator(this);
        textPass.setMediator(this);
        buttonOk.setMediator(this);
        buttonCancel.setMediator(this);
        // 设置Listener
        checkboxGuest.addItemListener(checkboxGuest);
        checkboxLogin.addItemListener(checkboxLogin);
        textUser.addTextListener(textUser);
        textPass.addTextListener(textPass);
        buttonOk.addActionListener(this);
        buttonCancel.addActionListener(this);
    }

    @Override
    public void colleagueChanged() {
        // 接受来自Colleague的通知后判断各Colleague的启用/禁用状态
        if (checkboxGuest.getState()) {
            // Guest mode
            textUser.setColleagueEnabled(false);
            textPass.setColleagueEnabled(false);
            buttonOk.setColleagueEnabled(true);
        } else {
            // User mode
            textUser.setColleagueEnabled(true);
            userPassChanged();
        }
    }

    /**
     * 当textUser或textPass文本输入框中的文字发生变化时，判断各Colleague的启用/禁用状态
     */
    private void userPassChanged() {
        if (textUser.getText().length() > 0) {
            textPass.setColleagueEnabled(true);
            buttonOk.setColleagueEnabled(textPass.getText().length() >= 4 && textUser.getText().length() >= 4);
        } else {
            textPass.setColleagueEnabled(false);
            buttonOk.setColleagueEnabled(false);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.toString());
        System.exit(0);
    }
}
